﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using DAO;
using BUS;

namespace SSN.MasterPages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string username = Request.QueryString["username"];
                string password = Request.QueryString["password"];

                KiemTraTaiKhoan(username, password);
            }
        }

        private int KiemTraTaiKhoan(string username, string pwd)
        {
            NguoiDungBUS nguoidungBUS = new NguoiDungBUS();
            NguoiDung ketqua = nguoidungBUS.LietKe().SingleOrDefault(n => n.TenDangNhap == username && n.MatKhau == HashPassword(pwd));
            if (ketqua != null)
            {
                Session["isLogin"] = true;
                Session["NguoiDung"] = ketqua;
                Response.Redirect("~/Loggedin/Home.aspx");
                return 1;
            }
            return 0;
        }

        private string HashPassword(string pwd)
        {
            HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();
            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pwd);
            byte[] bytHash = hashAlg.ComputeHash(bytValue);
            string hashedpwd = Convert.ToBase64String(bytHash);
            return hashedpwd;
        }
    }
}